package encaps.action;

import encaps.entities.Grant;
import encaps.resources.Labels;
import encaps.resources.Response;
import encaps.utils.StringUtil;
import encaps.utils.XMLHelper;
import org.jdom.Element;

public class ShowRegistrationPageAction extends EditProfileAction {

    public static final String SHOW_REGISTRATION_PAGE = "showRegistrationPage";
    public static final String PASSWORD = "pass";
    public static final String PASSWORD_REPEAT = "rePass";

    public ShowRegistrationPageAction(ActionContext context, Grant requiredGrant) {
        super(context, requiredGrant);
    }

    @Override
    public Element execute() throws ActionExecutionException {
        if (!StringUtil.isEmpty(actionContext.getUserLogin())) {
            return XMLHelper.createTextMessage(Labels.getLabel(Labels.ALREADY_LOGGED_MESSAGE));
        }

        Element response = XMLHelper.createElement(Response.RESPONSE);
        Element e = XMLHelper.addChild(response, Response.ENTITY);
        e.setAttribute(Response.EDIT, "true");

        XMLHelper.addChild(e, XMLHelper.createHeader(Labels.getLabel(Labels.REGISTRATION)));
        XMLHelper.addChild(e, XMLHelper.createImage("", PICTURE));
        XMLHelper.addChild(e, XMLHelper.createTextField(Labels.getLabel(Labels.LOGIN), null, LOGIN));
        XMLHelper.addChild(e, XMLHelper.createPasswordField(Labels.getLabel(Labels.PASSWORD), PASSWORD));
        XMLHelper.addChild(e, XMLHelper.createPasswordField(Labels.getLabel(Labels.REPEAT_PASSWORD), PASSWORD_REPEAT));
        XMLHelper.addChild(e, XMLHelper.createTextArea(Labels.getLabel(Labels.INFO), null, INFO));
        XMLHelper.addChild(e, XMLHelper.createTextField(Labels.getLabel(Labels.EMAIL), null, EMAIL));
        XMLHelper.addChild(e, XMLHelper.createTextField(Labels.getLabel(Labels.CONTACTS), null, CONTACTS));

        XMLHelper.addChild(e, XMLHelper.createButton(Labels.getLabel(Labels.REGISTER_BUTTON), RegistrationAction.REGISTER));

        return response;
    }
}
